package com.daimajia.swipedemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.daimajia.swipe.SwipeLayout;

import java.util.HashMap;
import java.util.Map;

public class SingleSwipeActivity extends Activity {
    private LinearLayout content;
    private static Map<String, SwipeLayout> controlSwipeMap = new HashMap<>();
    private SwipeLayout sample1, sample2, sample3;
    private String group = "111";
    private static boolean isUse = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_swipe);
        content = (LinearLayout) findViewById(R.id.content);
        sample1 = (SwipeLayout) findViewById(R.id.sample1);
        sample1.addDrag(SwipeLayout.DragEdge.Left, sample1.findViewById(R.id.bottom_wrapper));
        sample1.addDrag(SwipeLayout.DragEdge.Right, sample1.findViewById(R.id.bottom_wrapper_2));
        sample2 = (SwipeLayout) findViewById(R.id.sample2);
        sample2.addDrag(SwipeLayout.DragEdge.Left, sample2.findViewById(R.id.bottom_wrapper));
        sample2.addDrag(SwipeLayout.DragEdge.Right, sample2.findViewById(R.id.bottom_wrapper_2));
        sample3 = (SwipeLayout) findViewById(R.id.sample3);

        sample1.getSurfaceView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                if (isUse) return true;
                isUse = true;
                if((sample1.getOpenStatus()== SwipeLayout.Status.Close||sample1.getOpenStatus()== SwipeLayout.Status.Open)&&!sample1.isSwipeEnabled()){
                    sample1.setSwipeEnabled(true);
                }
//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).close();
//                    Log.d(SingleSwipeActivity.class.getSimpleName(), "controlSwipeMap.close==" + controlSwipeMap.get(group).toString());
//
//                }
//                controlSwipeMap.put(group, sample1);
//                sample1.setSwipeEnabled(true);
                Log.d(SingleSwipeActivity.class.getSimpleName(), "this.add==sample1");
                Log.d(SingleSwipeActivity.class.getSimpleName(), "this.sample1==onTouch");
                isUse = false;
                return false;
            }
        });

        sample2.getSurfaceView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                if (isUse) return true;
                isUse = true;
                if((sample2.getOpenStatus()== SwipeLayout.Status.Close||sample2.getOpenStatus()== SwipeLayout.Status.Open)&&!sample2.isSwipeEnabled()){
                    sample2.setSwipeEnabled(true);
                }
//                if (controlSwipeMap.get(group) != null) {
//                sample2.setSwipeEnabled(true);
//                }
//                controlSwipeMap.put(group, sample2);
                Log.d(SingleSwipeActivity.class.getSimpleName(), "this.add==sample2");
                Log.d(SingleSwipeActivity.class.getSimpleName(), "this.sample2==onTouch");
                isUse = false;
                return false;
            }
        });
        sample1.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
//                layout.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample1 ) {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.get(group).setSwipeEnabled(false);
//                            controlSwipeMap.remove(group);
////                            Log.d(SingleSwipeActivity.class.getSimpleName(), "controlSwipeMap.close==" + controlSwipeMap.get(group).toString());
//
//                        }
//
//                    }
//                },50);

                if(controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample1) {
//                    if (controlSwipeMap.get(group).getOpenStatus() == SwipeLayout.Status.Middle) {
//                        layout.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                controlSwipeMap.get(group).close();
//                                controlSwipeMap.get(group).setSwipeEnabled(false);
////                                controlSwipeMap.remove(group);
//                            }
//                        }, 80);
//                    } else {
                        controlSwipeMap.get(group).close();
                        controlSwipeMap.get(group).setSwipeEnabled(false);
//                        controlSwipeMap.remove(group);
//                    }
                }
//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.put(group,sample1);
//                        }
//                    });
//                    Log.d(SingleSwipeActivity.class.getSimpleName(),"controlSwipeMap.close=="+controlSwipeMap.get(group).toString());
//                }else {
//                    controlSwipeMap.put(group,sample1);
//                }
//
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.add==sample1");
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.sample1==onStartOpen");
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                if(controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample1){
                    controlSwipeMap.get(group).close();
                }
                    controlSwipeMap.put(group, sample1);
                layout.setSwipeEnabled(true);
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.sample1==onOpen");
            }

            @Override
            public void onStartClose(final SwipeLayout layout) {

//                sample1.setRightSwipeEnabled(false);
//                layout.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        layout.close();
//                        if (controlSwipeMap.get(group) != null) {
//                            controlSwipeMap.get(group).close();
//
//                        }
//                        return true;
//                    }
//                });
            }

            @Override
            public void onClose(SwipeLayout layout) {
                layout.setSwipeEnabled(true);
//                sample1.setRightSwipeEnabled(true);
//                layout.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        return false;
//                    }
//                });
//                layout.setOnTouchListener(null);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });


        sample2.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
//                layout.postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample2 ) {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.get(group).setSwipeEnabled(false);
//                            controlSwipeMap.remove(group);
////                            Log.d(SingleSwipeActivity.class.getSimpleName(), "controlSwipeMap.close==" + controlSwipeMap.get(group).toString());
//
//                        }
//
//                    }
//                },50);

                if(controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample2){
//                    if(controlSwipeMap.get(group).getOpenStatus()== SwipeLayout.Status.Middle ){
//                        layout.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                    controlSwipeMap.get(group).close();
//                                    controlSwipeMap.get(group).setSwipeEnabled(false);
////                                    controlSwipeMap.remove(group);
//                            }
//                        },80);
//                    }else {
                        controlSwipeMap.get(group).close();
                        controlSwipeMap.get(group).setSwipeEnabled(false);
//                        controlSwipeMap.remove(group);
//                    }

                }

//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.put(group,sample2);
//                        }
//                    });
//                    Log.d(SingleSwipeActivity.class.getSimpleName(),"controlSwipeMap.close=="+controlSwipeMap.get(group).toString());
//                }else {
//                    controlSwipeMap.put(group,sample2);
//                }
//
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.add==sample2");
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.sample2==onStartOpen");
            }

            @Override
            public void onOpen(SwipeLayout layout) {
                if(controlSwipeMap.get(group) != null&&controlSwipeMap.get(group)!=sample2){
                    controlSwipeMap.get(group).close();
                }
                controlSwipeMap.put(group, sample2);
                layout.setSwipeEnabled(true);
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.sample2==onOpen");
//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.put(group,sample2);
//                        }
//                    });
//                    Log.d(SingleSwipeActivity.class.getSimpleName(),"controlSwipeMap.close=="+controlSwipeMap.get(group).toString());
//                }else {
//                    controlSwipeMap.put(group,sample2);
//                }
//
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.add==sample2");
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.sample2==onStartOpen");
            }

            @Override
            public void onStartClose(final SwipeLayout layout) {
//                sample2.setRightSwipeEnabled(false);
//                layout.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        layout.close();
//                        if (controlSwipeMap.get(group) != null) {
//                            controlSwipeMap.get(group).close();
//
//                        }
//                        return true;
//                    }
//                });

            }

            @Override
            public void onClose(SwipeLayout layout) {
                layout.setSwipeEnabled(true);
//                sample2.setRightSwipeEnabled(true);
//                layout.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        return false;
//                    }
//                });
//                layout.setOnTouchListener(null);
            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });


        sample3.addSwipeListener(new SwipeLayout.SwipeListener() {
            @Override
            public void onStartOpen(SwipeLayout layout) {
//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.put(group,sample3);
//                        }
//                    });
//                    Log.d(SingleSwipeActivity.class.getSimpleName(),"controlSwipeMap.close=="+controlSwipeMap.get(group).toString());
//                }else {
//                    controlSwipeMap.put(group,sample3);
//                }
//
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.add==sample3");
            }

            @Override
            public void onOpen(SwipeLayout layout) {
//                if (controlSwipeMap.get(group) != null) {
//                    controlSwipeMap.get(group).post(new Runnable() {
//                        @Override
//                        public void run() {
//                            controlSwipeMap.get(group).close();
//                            controlSwipeMap.put(group,sample3);
//                        }
//                    });
//                    Log.d(SingleSwipeActivity.class.getSimpleName(),"controlSwipeMap.close=="+controlSwipeMap.get(group).toString());
//                }else {
//                    controlSwipeMap.put(group,sample3);
//                }
//
//                Log.d(SingleSwipeActivity.class.getSimpleName(),"this.add==sample3");
            }

            @Override
            public void onStartClose(SwipeLayout layout) {

            }

            @Override
            public void onClose(SwipeLayout layout) {

            }

            @Override
            public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {

            }

            @Override
            public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {

            }
        });
    }
}
